package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.Material;
import model.Proveedor;

public class ProveedorMapper implements DataMapper<Proveedor>{
	private static ProveedorMapper instance;
	
	private ProveedorMapper() {}
	
	public static ProveedorMapper getInstance() {
		if (instance == null) {
			instance = new ProveedorMapper();
		}
		return instance;
	}
	
	@Override
	public boolean insertar(Proveedor proveedor) {
		boolean result = false;
		Connection connection = PoolConnection.getPoolConnection().getConnection();

		try {
			String query = "INSERT INTO proveedor (codigo) VALUES (?)";
			PreparedStatement ps = connection.prepareStatement(query);
			ps.setString(1, proveedor.getCodigo());
			ps.execute();
			
			PoolConnection.getPoolConnection().releaseConnection(connection);
			result = true;
		} catch (SQLException e) {
			PoolConnection.getPoolConnection().releaseConnection(connection);
			e.printStackTrace();
		}
		
		return result;
	}

	@Override
	public boolean modificar(Proveedor proveedor) {
		return false;
	}

	@Override
	public boolean eliminar(Proveedor proveedor) {
		return false;
	}
	
	@Override
	public Proveedor buscar(Long proveedorId) {
		Proveedor proveedor = null;
		Connection con = PoolConnection.getPoolConnection().getConnection();

		try {
			String query = "SELECT * from proveedor where proveedor_id = ?";
			PreparedStatement ps = null;
			ps = con.prepareStatement(query);
			ps.setLong(1, proveedorId);
			ResultSet result = ps.executeQuery();
			
			if(result.next()) {
				proveedor = new Proveedor(result.getString("codigo"));
				proveedor.setProveedorId(result.getLong("proveedor_id"));
				List<Material> materiales = MaterialMapper.getInstance().buscarMaterialesProvistos(proveedor);
				proveedor.setMateriales(materiales);
			}
			
			PoolConnection.getPoolConnection().releaseConnection(con);

		} catch (SQLException e) {
			System.out.println("Mensaje Error al buscar Proveedor: " + e.getMessage());
			System.out.println("Stack Trace al buscar Proveedor: " + e.getStackTrace());
			PoolConnection.getPoolConnection().releaseConnection(con);
		}
		return proveedor;
	}

	@Override
	public List<Proveedor> buscarTodos() {
		List<Proveedor> proveedores = new ArrayList<Proveedor>();
		Connection con = PoolConnection.getPoolConnection().getConnection();

		try {
			String query = "SELECT * from proveedor";
			PreparedStatement ps = null;
			ps = con.prepareStatement(query);

			ResultSet result = ps.executeQuery();
			while(result.next()) {
				Proveedor proveedor = new Proveedor(result.getString("codigo"));
				proveedor.setProveedorId(result.getLong("proveedor_id"));
				
				List<Material> materiales = MaterialMapper.getInstance().buscarMaterialesProvistos(proveedor);
				proveedor.setMateriales(materiales);
				
				proveedores.add(proveedor);
			}
			PoolConnection.getPoolConnection().releaseConnection(con);

		} catch (SQLException e) {
			System.out.println("Mensaje Error Buscar todos los Proveedores: " + e.getMessage());
			System.out.println("Stack Trace al Buscar todos los Proveedores: " + e.getStackTrace());
			PoolConnection.getPoolConnection().releaseConnection(con);
		}
		return proveedores;
	}
	

	
	public static void main(String[] args) {
		ProveedorMapper instance = ProveedorMapper.getInstance();
		Proveedor proveedor = instance.buscar(2l);
		System.out.println(proveedor);
	}

}